/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INCLUDE_VBS_TRANSPORT_TCPHEADER_H_
#define INCLUDE_VBS_TRANSPORT_TCPHEADER_H_

#include <cstring>
#include <cstdint>

namespace vbs {
namespace transport {
#define RTCPHEADER_SIZE 8U
#define RTCP_HEADER_OFFSET 4U
#define RTCP_HEADER_ID_SIZE 4U
#define RTCP_HEADER_MIN_SIZE 8U
#define RTCP_HEADER_MAX_SIZE 64U

class RTCPHeader {
 public:
    RTCPHeader() : length_(RTCPHEADER_SIZE), offset_(RTCP_HEADER_OFFSET), used_default_header_(true) {
        memcpy(rtcp_, "RTCP", 4);
    }

    RTCPHeader(const char* rtcp, uint32_t _length, uint32_t _offset, bool _used_default_header = false)
        : length_(_length), offset_(_offset), used_default_header_(_used_default_header) {

        //如果配置出错直接使用默认的RTCP
        if (strlen(rtcp) != 4 || length_ < RTCP_HEADER_MIN_SIZE || offset_ + sizeof(uint32_t) > _length ||
            length_ > RTCP_HEADER_MAX_SIZE || length_ < RTCP_HEADER_MIN_SIZE) {
            memcpy(rtcp_, "RTCP", 4);
            length_ = RTCP_HEADER_MIN_SIZE;
            offset_ = RTCP_HEADER_OFFSET;
            used_default_header_ = true;
        } else {
            memcpy(rtcp_, rtcp, 4);
        }
    }

    // 新增的构造函数
    RTCPHeader(uint32_t rtcp_value, uint32_t _length, uint32_t _offset, bool _used_default_header = false)
        : length_(_length), offset_(_offset), used_default_header_(_used_default_header) {
        //如果配置出错直接使用默认的RTCP
        if (length_ < RTCP_HEADER_MIN_SIZE || offset_ + sizeof(uint32_t) > _length || length_ > RTCP_HEADER_MAX_SIZE ||
            length_ < RTCP_HEADER_MIN_SIZE) {
            memcpy(rtcp_, "RTCP", 4);
            length_ = RTCP_HEADER_MIN_SIZE;
            offset_ = RTCP_HEADER_OFFSET;
            used_default_header_ = true;
        } else {
            rtcp_[3] = static_cast<unsigned char>((rtcp_value >> 24) & 0xFF);
            rtcp_[2] = static_cast<unsigned char>((rtcp_value >> 16) & 0xFF);
            rtcp_[1] = static_cast<unsigned char>((rtcp_value >> 8) & 0xFF);
            rtcp_[0] = static_cast<unsigned char>(rtcp_value & 0xFF);
        }
    }

    ~RTCPHeader() = default;

    void set_length(uint32_t length) { length_ = length; }
    uint32_t get_length() const { return length_; }

    void set_offset(uint32_t offset) { offset_ = offset; }
    uint32_t get_offset() const { return offset_; }

    void set_rtcp(const char* rtcp) { memcpy(rtcp_, rtcp, 4); }
    const unsigned char* get_rtcp() const { return rtcp_; }

    uint32_t get_rtcp_header_ID_size() const { return RTCP_HEADER_ID_SIZE; }
    bool is_used_default_header() const { return used_default_header_; }

    void set_used_default_header(bool used_default_header) { used_default_header_ = used_default_header; }
    bool operator==(const RTCPHeader& other) const {
        return length_ == other.length_ && offset_ == other.offset_ &&
               used_default_header_ == other.used_default_header_ &&
               std::memcmp(rtcp_, other.rtcp_, RTCP_HEADER_ID_SIZE) == 0;
    }

 private:
    unsigned char rtcp_[4];
    uint32_t length_;
    uint32_t offset_;
    bool used_default_header_;
};
}  // namespace transport
}  // namespace vbs

#endif
